package com.winit.openapi.wh.outbound.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.oms.spi.outbound.OutboundOrderService;
import com.winit.oms.spi.outbound.command.order.QueryOrderBySellerCommand;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderFileExportTaskVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.wh.outbound.builder.OutboundOrderBuilder;
import com.winit.openapi.wh.outbound.validator.OutboundOrderfieldValidator;

/**
 * 海外仓出库单报表
 * 
 * @version <pre>
 * Author	  Version		 Date		 Changes
 * dinghua.an 	   1.0  		2017年5月24日 	 Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/outbound")
public class OutboundOrderReportController extends BaseController {

    @Resource
    private OutboundOrderService outboundOrderService;

    /**
     * 出库单列表查询.
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryReportList")
    public String queryReportList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryOrderBySellerCommand command = new QueryOrderBySellerCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            OutboundOrderfieldValidator.validQueryByMainOrderParam(json);
            command = OutboundOrderBuilder.buildOutboundCommand(json);
            Searchable<?> searchable = buildSearchablePageParams(json);
            searchable.addSort(new Sort(Direction.DESC, "CREATED"));
            command.setSearchable(searchable);
        } else if (obj == null || StringUtils.isBlank(obj.toString())) {
            Searchable<?> searchable = buildSearchablePageParams(null);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Page<OutboundOrderFileExportTaskVo> pagelist = outboundOrderService.queryReportList(command);
        if (pagelist != null && pagelist.getContent() != null && pagelist.getContent().size() > 0) {
            List<Map<String, Object>> datalist = buildOrderList(pagelist.getContent());
            Map<String, Object> pageMap = getPageParams(pagelist);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", datalist);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 拼装数据
     * 
     * @param datalist
     * @return
     */
    private List<Map<String, Object>> buildOrderList(List<OutboundOrderFileExportTaskVo> datalist) {
        List<Map<String, Object>> rslist = new ArrayList<Map<String, Object>>();
        for (OutboundOrderFileExportTaskVo vo : datalist) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("applyTime", vo.getApplyTime());
            map.put("remark", vo.getRemark());
            map.put("startTime", vo.getStartTime());
            map.put("endTime", vo.getEndTime());
            map.put("fileStatus", vo.getFileStatus());
            map.put("fileUrl", vo.getFileUrl());
            rslist.add(map);
        }
        return rslist;
    }

}
